package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.PostDTO;
import com.pet.platform.dto.PostQueryDTO;
import com.pet.platform.entity.Post;
import com.pet.platform.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/posts")
public class PostController {

    @Autowired
    private PostService postService;

    @GetMapping("/{id}")
    public Result<Post> findById(@PathVariable Long id) {
        return Result.success(postService.findById(id));
    }

    @GetMapping
    public Result<PageResult<PostDTO>> findPage(PostQueryDTO queryDTO) {
        return Result.success(postService.findPage(queryDTO));
    }

    @PostMapping
    public Result<Void> create(@Valid @RequestBody PostDTO postDTO) {
        postService.create(postDTO);
        return Result.success();
    }

    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @Valid @RequestBody PostDTO postDTO) {
        postService.update(id, postDTO);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        postService.delete(id);
        return Result.success();
    }

    @PostMapping("/{id}/like")
    public Result<Void> like(@PathVariable Long id) {
        postService.like(id);
        return Result.success();
    }

    @PostMapping("/{id}/unlike")
    public Result<Void> unlike(@PathVariable Long id) {
        postService.unlike(id);
        return Result.success();
    }
} 